package com.cz.basetool.ui_work.thread;

import android.util.Log;

import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;
import com.cz.basetool.ui_work.util.time.MyTimer;

import java.util.concurrent.CountDownLatch;

/**
 * Created by lianxiang on 2017/9/23.
 * 用于将异步回调结果用同步方法返回
 */

public class CZCountDownLatch extends CountDownLatch {
    private static final String TAG = "CZCountDownLatch";
    private boolean forceNotify = false;              //是否苏醒
    private long taskTimeoutMilliSeconds = 5 * 1000;    //业务 等待超时时间
    private final MyTimer timer;
    private int mySettedCount = 0;      //用于手动终止Latch。
//    private long sleepMilliSeconds = 1 * 1000;    //业务线程睡眠 超时时间

    public CZCountDownLatch(int count) {
        super(count);
        timer = new MyTimer();
        forceNotify = false;
        mySettedCount = count;
    }

    public CZCountDownLatch(int count, long taskTimeoutMilliSeconds) {       //指定业务的超时时间
        this(count);
        this.taskTimeoutMilliSeconds = taskTimeoutMilliSeconds;
    }

//    public CZCountDownLatch(int count, int mySettedCount, long taskTimeoutMilliSeconds, long
//            sleepMilliSeconds) {
//        this(count, taskTimeoutMilliSeconds);
//        this.sleepMilliSeconds = sleepMilliSeconds;
//    }

    public void setCount(int count) {
        mySettedCount = count;
    }

    @Override
    public void countDown() {
        super.countDown();
        mySettedCount--;
    }

    public int getMySettedCount() {
        return mySettedCount;
    }

    public void awaiting() throws Exception {
        if (getCount() <= 0) {
            return;
        }
        timer.setOverTimeListener(new MyTimer.OverTimeListener() {
            @Override
            public void OnOverTime(boolean isOverTime) {
                forceNotify = true;
            }
        });
        timer.startTimer(taskTimeoutMilliSeconds);
//        CZLogUtil.tempLogd(this, "当前线程：" + Thread.currentThread().getName() + " 休息" +
//                sleepMilliSeconds + "毫秒，让其它线程执行,mySettedCount = " + mySettedCount);
        AndroidConsoleLogPrinter.d("cz", "当前线程：" + Thread.currentThread().getName() + " 等待业务" +
                "让其它线程执行,count = " + mySettedCount);
//        long startTime = System.currentTimeMillis();
        while (getMySettedCount() != 0 && !forceNotify) {
            try {
//                if (System.currentTimeMillis() - startTime > 5000) {
//                    break;
//                }
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            try {
//                CZLogUtil.tempLogd(this, "当前线程：" + Thread.currentThread().getName() + " 休息" +
//                        sleepMilliSeconds + "毫秒，让其它线程执行,mySettedCount = " + mySettedCount);
//                Thread.sleep(sleepMilliSeconds);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }

        timer.stopTimer();

        //抛出异常
        if (forceNotify) {
            throw new  Exception("接口请求超时，请检查设备连接状态。。。");
        }
    }

    public long getTimeoutMilliSeconds() {
        return taskTimeoutMilliSeconds;
    }

    public void setTimeoutMilliSeconds(long taskTimeoutMilliSeconds) {
        this.taskTimeoutMilliSeconds = taskTimeoutMilliSeconds;
    }
}
